#include"../../source/message.hpp"

int main()
{

    if (Init_logger("DEBUG", "/srv/log/jsonRpc/", "message.log", 1024*1024))
    {
        std::cout << "日志初始成功" << std::endl;
    }
    else return -1;

    auto rpc1 = MessageFactory::create<RPCRequest>();
    rpc1->setId();
    rpc1->setType(MSG_Type::RPC_REQ);
    rpc1->setMethod("Add");
    Json::Value parames;
    parames["left"] = 1;
    parames["right"] = 2;
    rpc1->setParameters(parames);

    if (rpc1->check())
    {
        std::string str;
        rpc1->serialize(str);
        LOG_DEBUG << "rpc1 request 序列化后：\n" << str;
        LOG_DEBUG << "id >" << rpc1->getId();
    }

    auto rpc2 = MessageFactory::create<RpcResponse>();
    rpc2->setId();
    rpc2->setType(MSG_Type::RPC_RES);
    rpc2->setRcode(RCode::OK);
    Json::Value result;
    result[KEY_RESULT] = "hello";
    rpc2->setResult(result);
    if (rpc2->check())
    {
        std::string str;
        rpc2->serialize(str);
        LOG_DEBUG << "rpc2 response 序列化后：\n" << str;
        LOG_DEBUG << "id >" << rpc2->getId();
    }

    // Topic
    auto topic_request = MessageFactory::create<TopicRequest>();
    topic_request->setId();
    topic_request->setType(MSG_Type::TOPIC_REQ);
    topic_request->setTopic("news");
    topic_request->setOptype(TOPIC_OPTYPE::PUBLISH);
    topic_request->setContent("测试新闻");
    if (topic_request->check())
    {
        std::string str;
        topic_request->serialize(str);
        LOG_DEBUG << "topic_reauest 序列化后：\n" << str;
        LOG_DEBUG << "id = " << topic_request->getId();
    }

    auto topic_respose = MessageFactory::create<TopicResponse>();
    topic_respose->setId("123333333");
    topic_respose->setType(MSG_Type::TOPIC_RES);
    topic_respose->setRcode(RCode::OK);
    if (topic_respose->check())
    {
        std::string str;
        topic_respose->serialize(str);
        LOG_DEBUG << "topic_respose 序列化后：\n" << str;
        LOG_DEBUG << "id = " << topic_respose->getId();
    }

    // service
    auto ser_request = MessageFactory::create<ServiceRequest>();
    ser_request->setId();
    ser_request->setType(MSG_Type::SERVICE_REQ);
    ser_request->setOptype(SERVICE_OPTYPE::ONLINE);
    ser_request->setMethod("Add");
    ser_request->setHost(Address("127.0.0.1", 9090));
    if (ser_request->check())
    {
        std::string str;
        ser_request->serialize(str);
        LOG_DEBUG << "ser_request 序列化后：\n" << str;
        LOG_DEBUG << "id = " << ser_request->getId();
    }

    auto ser_response = MessageFactory::create<ServiceResponse>();
    ser_response->setId("1111111111");
    ser_response->setType(MSG_Type::SERVICE_RES);
    ser_response->setRcode(RCode::OK);
    ser_response->setOptype(SERVICE_OPTYPE::DISCOVERY);
    ser_response->setMethod("Add");
    std::vector<Address> hosts;
    hosts.push_back({"127.0.0.1", 9090});
    hosts.push_back({"127.0.0.2", 9091});
    ser_response->setHosts(hosts);
    if (ser_response->check())
    {
        std::string str;
        ser_response->serialize(str);
        LOG_DEBUG << "ser_response 序列化后：\n" << str;
        LOG_DEBUG << "id = " << ser_response->getId();
    }    

    sleep(10);
    return 0;
}